#ifndef __HTTP_RESPONSE_H__
#define __HTTP_RESPONSE_H__

#include "../buffer/buffer.h"

#include <sys/stat.h>
#include <sys/mman.h>
#include <unordered_map>
#include <string>
#include <fcntl.h>
#include <unistd.h>

class HttpResponse{
public:
    HttpResponse();
    ~HttpResponse();
    void Init(const std::string& srcDir, std::string path, bool isKeepAlive, int code);
    void MakeResponse(Buffer& buffer);

    void UnmapFile();

    char* File();
    size_t FileLen();
private:
    void AddStateLine(Buffer& buffer);
    void AddHeader(Buffer& buffer);
    void AddContent(Buffer& buffer);

    std::string GetFileType();
    void ErrorContent(Buffer& buffer, std::string msg);

    void ResetCode();
private:
    static const std::unordered_map<int, std::string> CODE_STATUS;
    static const std::unordered_map<int, std::string> ERROR_CODE_PATH;

    int code_;
    bool isKeepAlive_;
    std::string path_;
    std::string srcDir_;

    char* mmFile_;
    struct stat mmFileStat_;
};

#endif /* __HTTP_RESPONSE_H__ */
